(function() {
    "use strict";
    /**
     * @ngdoc function
     * @name webApp.controller:WebsiteCtrl
     * @description
     * # WebsiteCtrl
     * Controller of the webApp
     */
    angular.module("webApp").controller("WebsiteCtrl", WebsiteCtrl);

    WebsiteCtrl.$inject = [
        "$scope",
        "dataService",
        "$location",
        "$state",
        "helper",
        "$timeout",
        "$rootScope",
        "sku",
        "MENU_Data",
        "cacheService"
    ];

    function WebsiteCtrl(
        $scope,
        dataService,
        $location,
        $state,
        helper,
        $timeout,
        $rootScope,
        sku,
        MENU_Data,
        cacheService
    ) {
        $scope.websiteSample = {
            data: {
                homeSlider: [{
                        img: "https://img.alicdn.com/imgextra/i1/20841398/O1CN01f1f1l71MCKErUzpaI_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    },
                    {
                        img: "https://img.alicdn.com/imgextra/i1/20841398/O1CN01f1f1l71MCKErUzpaI_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    }
                ],
                homeCats: [1, 2],
                homeLinks: [{
                        img: "https://img.alicdn.com/imgextra/i2/20841398/O1CN01rLFeiT1MCKEriZtor_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    },
                    {
                        img: "https://img.alicdn.com/imgextra/i2/20841398/O1CN017pTHXc1MCKEpodnsh_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    },
                    {
                        img: "https://img.alicdn.com/imgextra/i2/20841398/O1CN017pTHXc1MCKEpodnsh_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    },
                    {
                        img: "https://img.alicdn.com/imgextra/i3/20841398/O1CN01LmbgRn1MCKEpocjMf_!!20841398.jpg",
                        url: "/detail/index",
                        query: []
                    }
                ],
                homeSerivces: [{
                    img: "https://img.alicdn.com/imgextra/i4/20841398/O1CN01gtLYpS1MCKDb1Ql6B_!!20841398.jpg",
                    url: "",
                    query: {}
                }],
                footer: {
                    comname: "公司名称",
                    icpcode: "icd备案号码",
                    gacode: "公安备案号",
                    logourl: "https://img.alicdn.com/imgextra/i3/20841398/O1CN01fOYnGr1MCKCCZ1XIV_!!20841398.gif"
                },
                xmodal: {
                    title: "消费者告知书",
                    content: "尊敬的消费者，根据财关税2016年18号《关于跨境电子商务零售进口税收政策的通知》、财关税2018年49号《关于完善跨境电子商务零售进口税收政策的通知》、海关总署194号公告《关于跨境电子商务零售进出口商品有关监管事宜的公告》等文件精神，跨境电商产品以及清关环节有以下注意事项："+
                    "\n1. 您通过本网站购买的所有跨境商品等同于境外购买，仅限个人自用，不得进行二次销售。"+
                    "\n2. 您购买的所有跨境商品相关的质量、健康、安全、卫生、环保、标识等项目标准符合国外相关标准或技术规范要求，但可能与我国质量安全标准存在差异，由此可能产生的危害或相关法律责任以及其他风险，将由您个人承担，故请了解相关信息后再做下单购买。"+
                    "\n3. 跨境电商平台购买的境外商品可能无中文标签或者中文说明书，请留意网站产品中文介绍或者联系客服人员咨询（商财发［2018］486号文）。"+
                    "\n4.订单清关环节，海关可能会抽查验核订单真实性，拨打购买人电话核实信息，必要时可能需要提供订购人身份证正反面复印件或者手持身份证照片验核交易真实性。"+
                    "\n5. 建议您对境外商品进行一定的了解并评估是否适合自己使用后再下单，感谢您的配合。",
                }
            }
        };
        //商品显示
        $scope.listAction = function() {
            $scope.getList();
        };

        //获取列表
        $scope.getList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id
            };
            dataService.websiteGet(query).then(function(list) {
                if (list) {
                    $scope.lists = list.list;
                    $scope.itemsTotal = list.count;
                }
            });
        };

        //删除
        $scope.del = function(id) {
            if (id) {
                var query = { wms_id: id, store_id: $rootScope.store_id };
                dataService.wmsDel(query).then(function(list) {
                    if (list.Msg) {
                        helper.myAlert("error" + list.Msg);
                    } else {
                        helper.myAlert("成功！");
                        $scope.getList();
                    }
                });
            }
        };

        //添加修改操作
        $scope.addAction = function() {
            $scope.chosedType = "slider";
            $scope.w_id = $location.search().w_id;
            if ($scope.w_id) {
                var query = {
                    w_id: $scope.w_id,
                    store_id: $rootScope.store_id
                };
                dataService.websiteGet(query).then(function(list) {
                    if (list) {
                        $scope.website = list.list[0];
                        $scope.website.data = JSON.parse($scope.website.data);
                        var cats = [];
                        _.map($scope.website.data.homeCats, function(da, key) {
                            if (da) {
                                cats.push({ id: da });
                            }
                        });
                        $scope.website.data.homeCats = cats;
                        $scope.website.data.homeSerivces =
                            $scope.website.data.homeSerivces ||
                            $scope.websiteSample.data.homeSerivces;
                        $scope.website.data.footer =
                            $scope.website.data.footer || $scope.websiteSample.data.footer;

                        console.error("$scope.website", $scope.website);
                    }
                });
            } else {
                $scope.website = $scope.websiteSample;
            }
        };
        $scope.savepath = function(path, submsg) {
            if (path) {
                submsg.url = path;
            }
        };
        $scope.itemdata = function(data, submsg) {
            if (data) {
                submsg.query = { item_id: data.item_id };
                submsg.url = "/detail/index";
            }
        };
        $scope.addMsg = function(type) {
            var add = {
                img: "https://res.vmallres.com/pimages//sale/2019-01/nEzinJ1ORDODLvMgQmmv.jpg",
                url: "/detail/index",
                query: []
            };
            if (type == 1) {
                $scope.website.data.homeSlider.push(add);
            }
            if (type == 2) {
                $scope.website.data.homeLinks.push(add);
            }
            if (type == 3) {
                $scope.website.data.homeSerivces.push(add);
            }
        };
        //添加标签
        $scope.websiteAdd = function() {
            if ($scope.website) {
                $scope.website.store_id = $rootScope.store_id;
                var data = clone($scope.website);
                var cats = [];
                _.map(data.data.homeCats, function(da, key) {
                    if (da.id) {
                        cats.push(da.id);
                    }
                });
                data.data.homeCats = cats;
                console.error("cats", cats);
                data.data = JSON.stringify(data.data);
                dataService.websiteAdd(data).then(function(list) {
                    if (!list.Msg) {
                        helper.myAlert("保存成功！", "#/website/list");
                    } else {
                        helper.myAlert("保存失败！");
                    }
                });
            }
        };
    }
})();